package com.XuYixuan;

import com.sun.source.tree.Tree;

import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        if(root == null){
            return ret;
        }

        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Integer> level = new ArrayList<Integer>();
//            int cursize = queue.size();
            for(int i = 0 ; i < queue.size() ; i++){
                TreeNode node = queue.poll();
                level.add(node.val);
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right);
                }
            }
            ret.add(level);
        }
        return ret;
    }
}

public class Main {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode left = new TreeNode(2);
        TreeNode right = new TreeNode(3);
        TreeNode leftLeft = new TreeNode(4);
        TreeNode leftRight = new TreeNode(5);
        root.left = left;
        root.right = right;
        left.left = leftLeft;
        left.right = leftRight;
        Solution solution = new Solution();
        List<List<Integer>> result = solution.levelOrder(root);
        System.out.println(result);
    }
}